package com.sunlands.zlcx.usercenter.domain;

import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import java.io.Serializable;
import java.util.Date;

@Data
@Entity(name = "tz_user_study_info")
@NoArgsConstructor
@AllArgsConstructor
@Builder
@DynamicUpdate
@DynamicInsert
public class UsersStudyInfoDO implements Serializable {

    private static final long serialVersionUID = 2781578996622311104L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    /**
     * 学习名称
     */
    private String studyInfoName;

    /**
     * 用户id
     */
    private Long userId;

    /**
     * 目标id
     */
    private Long targetId;

    /**
     * 状态
     */
    private Integer status;


    /**
     * 开始时间
     */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private Date startTime;

    /**
     * 结束时间
     */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private Date endTime;

    /**
     * 学习分钟数
     */
    private Integer studyMinutes;

    /**
     * 星期几
     */
    private String week;

    /**
     * 关联id
     */
    private Long relateId;

    /**
     * 创建时间
     */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private Date createTime;

    /**
     * 学习类型
     */
    private Integer type;

    /**
     * 统计时间
     */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private Date countTime;

    @SuppressWarnings("all")
    public enum TypeEnums {

        JOB_COUNT_TIME(101, "job_count_time", "定时器学习计时"),
        JOB_COUNT_TIME_START(111, "job_count_time_start", "定时器学习计时开始"),
        WEEK(0, "week", "星期"),
        ORDERNARY_COUNT_TIME(1, "ordernary_count_time", "学习计时"),
        LIAN_MAI(5, "lian_mai", "连麦计时"),

        ORDERNARY_COUNT_TIME_START(11, "学习计时开始", "专注计时"), // 不入库,有消息通知
        ORDERNARY_COUNT_TIME_PAUSE(13, "学习计时暂停", "专注计时"), // 不入库,无消息通知
        ORDERNARY_COUNT_TIME_CONTINUE(14, "学习计时继续", "专注计时"), // 不入库,无消息通知
        LIAN_MAI_START(15, "连麦学习开始", "连麦计时"), // 不入库,有消息通知


        SELECT_TARGET(25, "select_target", "选择目标"),
        ARCHIVE_TARGET(26, "archive_target", "归档目标"),
        DELETE_TARGET(27, "delete_target", "删除目标"),
        TIME_LEARN_ROOMING(60, "计时自习室学习开始", "自习室计时"),
        TIME_LEARN_ROOM_END(65, "计时自习室学习开始", "自习室计时"),
        ;

        public final Integer code;
        public final String desc;
        public final String title;

        TypeEnums(Integer code, String desc, String title) {
            this.code = code;
            this.desc = desc;
            this.title = title;
        }

        public static UsersStudyInfoDO.TypeEnums create(Integer code) {
            if (null == code) {
                return null;
            }
            for (UsersStudyInfoDO.TypeEnums field : UsersStudyInfoDO.TypeEnums.values()) {
                if (code.intValue() == field.getCode()) {
                    return field;
                }
            }
            return null;
        }

        public static boolean ifTarget(Integer code) {
            if (null == code) {
                return false;
            } else if (24 < code && code < 28) {
                return true;
            }
            return false;
        }

        public static boolean ifLearnTiming(Integer code) {
            if (null == code) {
                return false;
            } else if (0 < code && code < 10) {
                return true;
            }
            return false;
        }

        public static boolean ifStartLearn(Integer code) {
            if (null == code) {
                return false;
            } else if (create(code) == ORDERNARY_COUNT_TIME_START || create(code) == LIAN_MAI_START) {
                return true;
            }
            return false;
        }

        public Integer getCode() {
            return code;
        }

        public String getDesc() {
            return desc;
        }

        public String getTitle() {
            return title;
        }
    }

}
